<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>docker监控 | 如是的学习博客</title><meta name="keywords" content="docker"><meta name="author" content="rushiy"><meta name="copyright" content="rushiy"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="监控系统概述：  cAdvisor(container cAdvisor)：用于收集正在运行的容器资源使用和性能信息，在每个docker 主机上部署   https:&#x2F;&#x2F;github.com&#x2F;google&#x2F;cAdvisor  Prometheus：容器监控系统，从cAdvisor收集并存储数据，官网https:&#x2F;&#x2F;prometheus.io&#x2F;  Grafana：开源可视化展示系统，官网https:">
<meta property="og:type" content="article">
<meta property="og:title" content="docker监控">
<meta property="og:url" content="https://rushiy.gitee.io/docker/docker%E7%9B%91%E6%8E%A7/index.html">
<meta property="og:site_name" content="如是的学习博客">
<meta property="og:description" content="监控系统概述：  cAdvisor(container cAdvisor)：用于收集正在运行的容器资源使用和性能信息，在每个docker 主机上部署   https:&#x2F;&#x2F;github.com&#x2F;google&#x2F;cAdvisor  Prometheus：容器监控系统，从cAdvisor收集并存储数据，官网https:&#x2F;&#x2F;prometheus.io&#x2F;  Grafana：开源可视化展示系统，官网https:">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-09-24T15:27:51.000Z">
<meta property="article:author" content="rushiy">
<meta property="article:tag" content="docker">
<meta name="twitter:card" content="summary"><link rel="shortcut icon" href="/img/favicon.jpg"><link rel="canonical" href="https://rushiy.gitee.io/docker/docker%E7%9B%91%E6%8E%A7/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'docker监控',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-11-19 01:18:40'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/favicon.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">28</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">3</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">如是的学习博客</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">docker监控</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-09-24T15:27:51.000Z" title="发表于 2022-09-24 23:27:51">2022-09-24</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-11-18T17:18:40.829Z" title="更新于 2022-11-19 01:18:40">2022-11-19</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="docker监控"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>监控系统概述：</p>
<ul>
<li><p>cAdvisor(container cAdvisor)：用于收集正在运行的容器资源使用和性能信息，在每个docker 主机上部署</p>
<p>  <a target="_blank" rel="noopener" href="https://github.com/google/cAdvisor">https://github.com/google/cAdvisor</a></p>
</li>
<li><p>Prometheus：容器监控系统，从cAdvisor收集并存储数据，官网<a target="_blank" rel="noopener" href="https://prometheus.io/">https://prometheus.io/</a></p>
</li>
<li><p>Grafana：开源可视化展示系统，官网<a target="_blank" rel="noopener" href="https://grafana.com/grafana">https://grafana.com/grafana</a></p>
</li>
</ul>
<h2 id="配置cAdvisor"><a href="#配置cAdvisor" class="headerlink" title="配置cAdvisor"></a>配置cAdvisor</h2><h3 id="安装cAdvisor"><a href="#安装cAdvisor" class="headerlink" title="安装cAdvisor"></a>安装cAdvisor</h3><p>可通过<code>GitHub</code>中<code>cAdvisor</code>的运行容器命令，运行<code>cAdvisor</code></p>
<img src="/pic/docker-pic/image-20210405163157209.png" alt="image-20210405163157209" style="zoom:67%;" />

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker run \</span></span><br><span class="line"><span class="language-bash">  --volume=/:/rootfs:ro \</span></span><br><span class="line"><span class="language-bash">  --volume=/var/run:/var/run:ro \</span></span><br><span class="line"><span class="language-bash">  --volume=/sys:/sys:ro \</span></span><br><span class="line"><span class="language-bash">  --volume=/var/lib/docker/:/var/lib/docker:ro \</span></span><br><span class="line"><span class="language-bash">  --volume=/dev/disk/:/dev/disk:ro \</span></span><br><span class="line"><span class="language-bash">  --publish=8080:8080 \</span></span><br><span class="line"><span class="language-bash">  --detach=<span class="literal">true</span> \</span></span><br><span class="line"><span class="language-bash">  --name=cadvisor \</span></span><br><span class="line"><span class="language-bash">  --privileged \</span></span><br><span class="line"><span class="language-bash">  --device=/dev/kmsg \</span></span><br><span class="line"><span class="language-bash">  google/cadvisor:latest     <span class="comment"># Github中的源地址可能访问不了，因此这里更换成dockerhub中的地址</span></span></span><br><span class="line">Unable to find image &#x27;google/cadvisor:latest&#x27; locally</span><br><span class="line">latest: Pulling from google/cadvisor</span><br><span class="line">ff3a5c916c92: Downloading </span><br><span class="line">44a45bb65cdf: Download complete </span><br><span class="line">0bbe1a2fe2a6: Download complete </span><br><span class="line">latest: Pulling from google/cadvisor</span><br><span class="line">ff3a5c916c92: Pull complete </span><br><span class="line">44a45bb65cdf: Pull complete </span><br><span class="line">0bbe1a2fe2a6: Pull complete </span><br><span class="line">Digest: sha256:815386ebbe9a3490f38785ab11bda34ec8dacf4634af77b8912832d4f85dca04</span><br><span class="line">Status: Downloaded newer image for google/cadvisor:latest</span><br><span class="line">WARNING: IPv4 forwarding is disabled. Networking will not work.  # 此处有一个报错，需要打开路由转发功能</span><br><span class="line">e1132c19a4129804a28c1c0d0d61884ced3d548625daf882834095a420d3da7a</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker container <span class="built_in">ls</span></span> </span><br><span class="line">CONTAINER ID   IMAGE                    COMMAND                  CREATED          STATUS                          PORTS                                NAMES</span><br><span class="line">886e2bce936f   google/cadvisor:latest   &quot;/usr/bin/cadvisor -…&quot;   12 seconds ago   Up 11 seconds                   0.0.0.0:8080-&gt;8080/tcp               cadvisor</span><br><span class="line">60d4d688b698   php:lnmp_php_7.4.16      &quot;./sbin/php-fpm -c /…&quot;   6 hours ago      Restarting (78) 3 seconds ago                                        lnmp_php</span><br><span class="line">10124a1d6b8b   mysql:5.7                &quot;docker-entrypoint.s…&quot;   16 hours ago     Up 7 hours                      33060/tcp, 0.0.0.0:13306-&gt;3306/tcp   lnmp_mysql</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="访问-cAdvisor"><a href="#访问-cAdvisor" class="headerlink" title="访问 cAdvisor"></a>访问 <code>cAdvisor</code></h3><p>直接在浏览器中输入url–<code>http://10.138.3.130:8080</code>访问<code>cAdvisor</code>，访问较慢 </p>
<p><img src="/pic/docker-pic/image-20210405214538925.png" alt="image-20210405214538925"></p>
<blockquote>
<p>第一次实验的时候一直不能通过<a target="_blank" rel="noopener" href="http://10.138.3.130:8080/%E8%AE%BF%E9%97%AE%EF%BC%8C%E9%87%8D%E6%96%B0%E5%88%A0%E9%99%A4%E5%B9%B6%E5%AE%89%E8%A3%85cAdvisor%E5%AE%B9%E5%99%A8%EF%BC%8C%E5%8F%91%E7%8E%B0%E6%89%A7%E8%A1%8C%E5%AE%8C%E6%96%B0%E5%BB%BAcAdvisor%E5%AE%B9%E5%99%A8%E5%90%8E%E6%9C%89%E4%B8%80%E4%B8%AA%E6%8A%A5%E9%94%99%E4%BF%A1%E6%81%AF%60WARNING">http://10.138.3.130:8080/访问，重新删除并安装cAdvisor容器，发现执行完新建cAdvisor容器后有一个报错信息`WARNING</a>: IPv4 forwarding is disabled. Networking will not work.<code>，解决方法是打开路由转发功能</code>echo ‘net.ipv4.ip_forward&#x3D;1’ &gt;&gt; &#x2F;usr&#x2F;lib&#x2F;sysctl.d&#x2F;00-system.conf&#96;</p>
</blockquote>
<p>查看宿主机上运行的容器信息：</p>
<p><img src="/pic/docker-pic/image-20210405214435116.png" alt="image-20210405214435116"></p>
<h2 id="配置prometheus"><a href="#配置prometheus" class="headerlink" title="配置prometheus"></a>配置prometheus</h2><h3 id="安装prometheus"><a href="#安装prometheus" class="headerlink" title="安装prometheus"></a>安装prometheus</h3><p> <code>prometheus</code>可部署在任意的宿主机上，<code> cAdvisor</code>暴露出来的指标必须要符合<code>prometheus</code>的格式，才能被<code>prometheus</code>采集，我们在浏览器中输入<code>http://10.138.3.130:8080/metrics</code>，即可查看到符合<code>prometheus</code>格式的信息</p>
<p><img src="/pic/docker-pic/image-20210405214835081.png" alt="image-20210405214835081"></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">安装prometheus</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker run -d --name prometheus -p 9090:9090 prom/prometheus</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker ps -l</span> </span><br><span class="line">CONTAINER ID   IMAGE             COMMAND                  CREATED         STATUS         PORTS                    NAMES</span><br><span class="line">fe3468745563   prom/prometheus   &quot;/bin/prometheus --c…&quot;   6 seconds ago   Up 5 seconds   0.0.0.0:9090-&gt;9090/tcp   prometheus</span><br></pre></td></tr></table></figure>

<h3 id="访问prometheus"><a href="#访问prometheus" class="headerlink" title="访问prometheus"></a>访问prometheus</h3><p>在浏览器中访问<code>http://ip:9090/config</code>，如下：</p>
<img src="/pic/docker-pic/image-20210405215848324.png" alt="image-20210405215848324" style="zoom: 67%;" />

<h3 id="添加监控主机"><a href="#添加监控主机" class="headerlink" title="添加监控主机"></a>添加监控主机</h3><p>修改<code>prometheus</code>配置文件，让<code>prometheus</code>采集数据，每一个被监控端的信息都需要写入<code>prometheus</code>的配置文件，<code>prometheus</code>才能采集到数据，更改配置文件就是让<code>prometheus</code>知道去哪里取数据。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">进入prometheus容器中</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker <span class="built_in">exec</span> -it fe3468745563 sh</span> </span><br><span class="line">/prometheus $ vi /etc/prometheus/prometheus.yml </span><br><span class="line">scrape_configs:                                                    </span><br><span class="line"><span class="meta prompt_">  # </span><span class="language-bash">The job name is added as a label `job=&lt;job_name&gt;` to any timeseries scraped from this config.</span></span><br><span class="line">  - job_name: &#x27;prometheus&#x27;     # 等同于分组的组名                                                                                                               </span><br><span class="line">    static_configs:                      </span><br><span class="line">    - targets: [&#x27;localhost:9090&#x27;]       # 被监控的目标端 </span><br><span class="line">                                 </span><br><span class="line">  - job_name: &#x27;docker&#x27;           # 新建一个docker分组</span><br><span class="line">                                 </span><br><span class="line">    static_configs:              </span><br><span class="line">    - targets: [&#x27;10.138.3.130:8080&#x27;]    # 添加cAdvisor的接口，就能监控到数据；多个被监控端，用逗号隔开</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">重启生效</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker container restart  fe3468745563</span> </span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker ps -l</span> </span><br><span class="line">CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS          PORTS                    NAMES</span><br><span class="line">fe3468745563   prom/prometheus   &quot;/bin/prometheus --c…&quot;   17 minutes ago   Up 14 seconds   0.0.0.0:9090-&gt;9090/tcp   prometheus</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>​        此时就能在<code>prometheus</code>的<code>targets</code>页面查看新添加的主机信息：</p>
<p><img src="/pic/docker-pic/image-20210405221649388.png" alt="image-20210405221649388"></p>
<p>现在我们可以通过在<code>prometheus</code>的<code>Graph</code>页面输入<code>promql</code>查看指定的性能数据了</p>
<p><img src="/pic/docker-pic/image-20210405221756180.png" alt="image-20210405221756180"></p>
<p>如下，这里只要输入一个字符，就能列出跟含有该字符的所有的<code>promql</code>，非常方便</p>
<p><img src="/pic/docker-pic/image-20210405221946712.png" alt="image-20210405221946712"></p>
<h2 id="配置Grafana"><a href="#配置Grafana" class="headerlink" title="配置Grafana"></a>配置Grafana</h2><h3 id="安装Grafana"><a href="#安装Grafana" class="headerlink" title="安装Grafana"></a>安装Grafana</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker run -d --name grafana -p 3000:3000 grafana/grafana</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker ps -l</span> </span><br><span class="line">CONTAINER ID   IMAGE             COMMAND     CREATED          STATUS          PORTS                    NAMES</span><br><span class="line">99ec838588e1   grafana/grafana   &quot;/run.sh&quot;   39 seconds ago   Up 37 seconds   0.0.0.0:3000-&gt;3000/tcp   grafana</span><br></pre></td></tr></table></figure>

<h3 id="登陆Grafana"><a href="#登陆Grafana" class="headerlink" title="登陆Grafana"></a>登陆Grafana</h3><p>登陆grafana，用户和密码都是admin，登陆后会要求修改密码。</p>
<p><img src="/pic/docker-pic/image-20210405222545189.png" alt="image-20210405222545189"></p>
<p>修改密码后(Admin)，进入主页：</p>
<p><img src="/pic/docker-pic/image-20210405222915458.png" alt="image-20210405222915458"></p>
<h3 id="配置Grafana-1"><a href="#配置Grafana-1" class="headerlink" title="配置Grafana"></a>配置Grafana</h3><h4 id="添加数据源–Prometheus"><a href="#添加数据源–Prometheus" class="headerlink" title="添加数据源–Prometheus"></a>添加数据源–Prometheus</h4><p>首先，添加一个数据源–即告诉<code>Grafana</code>去哪里获取数据，点击<code>DATA SOURCES</code>，使<code>grafana</code>连接到<code>prometheus</code>，下图页面展示了所有能添加的数据源。</p>
<p><img src="/pic/docker-pic/image-20210405223215419.png" alt="image-20210405223215419"></p>
<p>输入Prometheus的访问地址，并保存。</p>
<p><img src="/pic/docker-pic/image-20210405223339334.png" alt="image-20210405223339334"></p>
<p>查看添加的数据源信息：</p>
<p><img src="/pic/docker-pic/image-20210405223451030.png" alt="image-20210405223451030"></p>
<h4 id="添加自定义仪表盘"><a href="#添加自定义仪表盘" class="headerlink" title="添加自定义仪表盘"></a>添加自定义仪表盘</h4><p><img src="/pic/docker-pic/image-20210405223545260.png" alt="image-20210405223545260"></p>
<p>默认的仪表盘页面(更正：下文中截图Prometheus的查询语句promql，误写成了proql)如下：</p>
<p><img src="/pic/docker-pic/image-20210405224051163.png" alt="image-20210405224051163"></p>
<p>此时我们即将使用到<code>promql</code>，即将<code>promql</code>添加到<code>Grafana</code>仪表盘中的<code>Metrics</code>中<code>Grafana</code>将在该图形中展示哪种<code>promethues</code>中的数据，我们依旧可以通过<code>prometheus</code>界面上的<code>Graph</code>进行调试，如监控内存，我们列出所有跟容器相关的<code>container_memory_usage_bytes</code>的信息</p>
<p><img src="/pic/docker-pic/image-20210405224523020.png" alt="image-20210405224523020"></p>
<p>下面，我们通过筛选条件，列出容器的<code>container_memory_usage_bytes</code>信息，<code>promql</code>语句为<code>container_memory_usage_bytes&#123;image!=&quot;&quot;&#125;</code></p>
<p><img src="/pic/docker-pic/image-20210405224757286.png" alt="image-20210405224757286"></p>
<p>接下来，我们采集具体的容器，如名称为<code>cAdvisor</code>的容器的信息，<code>promql</code>为<code>container_memory_usage_bytes&#123;image!=&quot;&quot;,name=&quot;cadvisor&quot;&#125;</code>，如下</p>
<p><img src="/pic/docker-pic/image-20210405225046812.png" alt="image-20210405225046812"></p>
<p>将上面得到的<code>promql</code>写入到<code>grafana</code>中的<code>metrics</code>，就能得到数据</p>
<p><img src="/pic/docker-pic/image-20210405230150973.png" alt="image-20210405230150973"></p>
<p>完成后，如下图所示：</p>
<p><img src="/pic/docker-pic/image-20210405230244625.png" alt="image-20210405230244625"></p>
<h4 id="添加模板仪表盘"><a href="#添加模板仪表盘" class="headerlink" title="添加模板仪表盘"></a>添加模板仪表盘</h4><p>如果每台机器都要这样手动配置一个图形的话，对于十来台服务器来说还可以接受，但对于成有千上万台的服务器的公司来说，这样的配置方式无疑是灾难性的，因此有没有一个通用的模板呢？答案是肯定的，<code>grafana</code>官网提供了很多这样的监控模板，访问地址–<code>https://grafana.com/grafana/dashboards/</code>：</p>
<p><img src="/pic/docker-pic/image-20210405230718697.png" alt="image-20210405230718697"></p>
<p>监控docker主机模板ID：193，即使用url–<code>https://grafana.com/grafana/dashboards/193</code></p>
<p><img src="/pic/docker-pic/image-20210405231133723.png" alt="image-20210405231133723"></p>
<p>在grafana首页中，点击<code>manage</code>–&gt;<code>import</code>，输入<code>193</code>导入模板</p>
<p><img src="/pic/docker-pic/image-20210405231439212.png" alt="image-20210405231439212"></p>
<p>输入<code>193</code>点击<code>load</code>按钮</p>
<p><img src="/pic/docker-pic/image-20210405231558075.png" alt="image-20210405231558075"></p>
<p>点击<code>load</code>后，自动加载</p>
<p><img src="/pic/docker-pic/image-20210405231650128.png" alt="image-20210405231650128"></p>
<p>导入后，即可看到仪表盘数据内容</p>
<p><img src="/pic/docker-pic/image-20210405231742854.png" alt="image-20210405231742854"></p>
<p>这里的仪表盘只是大佬给我们做好了的模板，本质上也是通过<code>promql</code>自定义仪表盘来做的，例如，我们查看上图中的<code>Total Memory Usage</code>，点击<code>Edit</code>来到编辑界面</p>
<p><img src="/pic/docker-pic/image-20210405232056122.png" alt="image-20210405232056122"></p>
<p>​        <code>promql</code>语句如下：</p>
<p><img src="/pic/docker-pic/image-20210405232158441.png" alt="image-20210405232158441"></p>
<h3 id="一些常见的坑"><a href="#一些常见的坑" class="headerlink" title="一些常见的坑"></a>一些常见的坑</h3><ul>
<li>不展示数据：<ul>
<li>可能是宿主机由于时间不同步</li>
<li>promql格式有问题，可以通过<code>prometheus</code>的<code>Graph</code>进行检查</li>
<li>prometheus本来就没有数据</li>
</ul>
</li>
</ul>
<h3 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h3><p>cAdvisor+prometheus+grafana进行监控的步骤总结：</p>
<ol>
<li>在被监控主机上安装cAdvisor容器，让cAdvisor收集宿主机数据</li>
<li>将宿主机监控端写入prometheus配置文件，让Prometheus能够获得cAdvisor采集到宿主机的监控数据</li>
<li>添加到grafana仪表盘(但是每天主机都添加一个仪表盘是不是很烦呢？能否一个界面显示所有的主机呢？需要查看哪台主机就点击哪台主机呢？)</li>
</ol>
<p>点击下图中的<code>设置</code>按钮，进入设置选项</p>
<p><img src="/pic/docker-pic/image-20210405233132149.png" alt="image-20210405233132149"></p>
<p>点击<code>Versions</code>，添加变量</p>
<p><img src="/pic/docker-pic/image-20210405233216163.png" alt="image-20210405233216163"></p>
<p>如下，列出所有分组(或主机)信息，即<code>dashboard</code>中是按照<code>job</code>还是<code>instance</code>显示主机：</p>
<p><img src="/pic/docker-pic/image-20210405233844242.png" alt="image-20210405233844242"></p>
<p>上图中的配置，我们仍然是通过<code>promql</code>实现，在<code>Graph</code>页面显示不出的数据，不可能在<code>grafana</code>中能显示图形。</p>
<p><img src="/pic/docker-pic/image-20210405233532485.png" alt="image-20210405233532485"></p>
<p>此时，查看<code>grafana</code>仪表盘，就多了选择框，可以选择主机或分组，上图中<code>add variables</code>(添加变量)时使用 的是<code>instance</code>就显示所有主机的信息，添加的是<code>job</code>就会显示所有分组的信息。</p>
<p><img src="/pic/docker-pic/image-20210405234100094.png" alt="image-20210405234100094"></p>
<p>但是，现在我们选择两个不同的节点，图表的结果是一样的，因为<code>promql</code>中的语句写死了，没有采集到对应<code>node</code>节点主机的信息，因此需要修改，方法如下，先点击进入其中一个仪表盘的修改界面：</p>
<p><img src="/pic/docker-pic/image-20210405234556293.png" alt="image-20210405234556293"></p>
<p>添加上每台主机，在原<code>promql</code>的基础上，添加上<code>&quot;instance=&#39;$Node&#39;&quot;</code>，变量<code>Node</code>是我们上文中通过<code>add variables</code>自定义的变量名称</p>
<p><img src="/pic/docker-pic/image-20210405234802713.png" alt="image-20210405234802713"></p>
<p>通过这样的修改，得到的就是每一台主机的<code>内存使用量</code>了，然后再将该仪表盘中的其他仪表盘<code>promql</code>都进行这样的修改，就能展示出每台主机独特的信息。 </p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://rushiy.gitee.io">rushiy</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://rushiy.gitee.io/docker/docker%E7%9B%91%E6%8E%A7/">https://rushiy.gitee.io/docker/docker%E7%9B%91%E6%8E%A7/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://rushiy.gitee.io" target="_blank">如是的学习博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/docker/">docker</a></div><div class="post_share"><div class="social-share" data-image="" data-sites="wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/favicon.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">rushiy</div><div class="author-info__description"></div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">28</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">3</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div><div class="card-info-social-icons is-center"><a class="social-icon" href="mailto:rushiy@reg.email.cn" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%85%8D%E7%BD%AEcAdvisor"><span class="toc-number">1.</span> <span class="toc-text">配置cAdvisor</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%89%E8%A3%85cAdvisor"><span class="toc-number">1.1.</span> <span class="toc-text">安装cAdvisor</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%AE%BF%E9%97%AE-cAdvisor"><span class="toc-number">1.2.</span> <span class="toc-text">访问 cAdvisor</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%85%8D%E7%BD%AEprometheus"><span class="toc-number">2.</span> <span class="toc-text">配置prometheus</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%89%E8%A3%85prometheus"><span class="toc-number">2.1.</span> <span class="toc-text">安装prometheus</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%AE%BF%E9%97%AEprometheus"><span class="toc-number">2.2.</span> <span class="toc-text">访问prometheus</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B7%BB%E5%8A%A0%E7%9B%91%E6%8E%A7%E4%B8%BB%E6%9C%BA"><span class="toc-number">2.3.</span> <span class="toc-text">添加监控主机</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%85%8D%E7%BD%AEGrafana"><span class="toc-number">3.</span> <span class="toc-text">配置Grafana</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%89%E8%A3%85Grafana"><span class="toc-number">3.1.</span> <span class="toc-text">安装Grafana</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%99%BB%E9%99%86Grafana"><span class="toc-number">3.2.</span> <span class="toc-text">登陆Grafana</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%85%8D%E7%BD%AEGrafana-1"><span class="toc-number">3.3.</span> <span class="toc-text">配置Grafana</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%B7%BB%E5%8A%A0%E6%95%B0%E6%8D%AE%E6%BA%90%E2%80%93Prometheus"><span class="toc-number">3.3.1.</span> <span class="toc-text">添加数据源–Prometheus</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%B7%BB%E5%8A%A0%E8%87%AA%E5%AE%9A%E4%B9%89%E4%BB%AA%E8%A1%A8%E7%9B%98"><span class="toc-number">3.3.2.</span> <span class="toc-text">添加自定义仪表盘</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%B7%BB%E5%8A%A0%E6%A8%A1%E6%9D%BF%E4%BB%AA%E8%A1%A8%E7%9B%98"><span class="toc-number">3.3.3.</span> <span class="toc-text">添加模板仪表盘</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%80%E4%BA%9B%E5%B8%B8%E8%A7%81%E7%9A%84%E5%9D%91"><span class="toc-number">3.4.</span> <span class="toc-text">一些常见的坑</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%80%BB%E7%BB%93"><span class="toc-number">3.5.</span> <span class="toc-text">总结</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/django/Django%E6%A8%A1%E6%9D%BF%E7%B3%BB%E7%BB%9F/" title="Django模板系统"><img src="" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Django模板系统"/></a><div class="content"><a class="title" href="/django/Django%E6%A8%A1%E6%9D%BF%E7%B3%BB%E7%BB%9F/" title="Django模板系统">Django模板系统</a><time datetime="2022-11-04T07:08:03.000Z" title="发表于 2022-11-04 15:08:03">2022-11-04</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/django/Django%E8%A7%86%E5%9B%BE/" title="Django视图"><img src="" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Django视图"/></a><div class="content"><a class="title" href="/django/Django%E8%A7%86%E5%9B%BE/" title="Django视图">Django视图</a><time datetime="2022-11-03T07:07:51.000Z" title="发表于 2022-11-03 15:07:51">2022-11-03</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/django/Django%E8%B7%AF%E7%94%B1%E7%B3%BB%E7%BB%9F/" title="Django路由系统"><img src="" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Django路由系统"/></a><div class="content"><a class="title" href="/django/Django%E8%B7%AF%E7%94%B1%E7%B3%BB%E7%BB%9F/" title="Django路由系统">Django路由系统</a><time datetime="2022-11-02T07:07:34.000Z" title="发表于 2022-11-02 15:07:34">2022-11-02</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/django/%E8%B5%B0%E8%BF%9BDjango/" title="走进Django"><img src="" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="走进Django"/></a><div class="content"><a class="title" href="/django/%E8%B5%B0%E8%BF%9BDjango/" title="走进Django">走进Django</a><time datetime="2022-11-01T07:01:20.000Z" title="发表于 2022-11-01 15:01:20">2022-11-01</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/python/python%E5%B8%B8%E7%94%A8%E6%A8%A1%E5%9D%97/" title="python常用模块"><img src="" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="python常用模块"/></a><div class="content"><a class="title" href="/python/python%E5%B8%B8%E7%94%A8%E6%A8%A1%E5%9D%97/" title="python常用模块">python常用模块</a><time datetime="2022-10-28T15:34:07.000Z" title="发表于 2022-10-28 23:34:07">2022-10-28</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2022 By rushiy</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><div class="js-pjax"></div><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>